package com.linkhub.oms.admin.modules.pickuporder.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.EntryOrderStateChangeParam;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.MarkCompletionParam;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.PickUpOrderQueryParam;
import com.linkhub.oms.admin.modules.pickuporder.domain.param.PickUpOrderSaveOrUpdateParam;
import com.linkhub.oms.admin.modules.pickuporder.domain.vo.EntryOrderLineImportResultVO;
import com.linkhub.oms.admin.modules.pickuporder.domain.vo.EntryOrderResultVO;
import com.linkhub.oms.admin.modules.pickuporder.domain.vo.PickUpOrderDetailVO;
import com.linkhub.oms.admin.modules.pickuporder.domain.vo.PickUpOrderVO;
import com.linkhub.oms.admin.modules.pickuporder.service.PickUpOrderBizService;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.enums.mp.RoleCodeEnum;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: PickUpOrderController
 * @Author: echo
 * @Date: 2024/5/2 09:40
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"提货管理模块"})
@RequestMapping("/pick_up/order")
@SaCheckLogin
public class PickUpOrderController {

    @Resource
    private PickUpOrderBizService pickUpOrderBizService;

    @GetMapping("/list")
    @ApiOperation(value = "查询提货申请单列表接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<PageDTO<PickUpOrderVO>> pageOrders(@Validated PickUpOrderQueryParam orderQueryParam) {
        List<String> userRoles = LhSessionContext.getUserRoles();
        orderQueryParam.setCustomerRole(userRoles.contains(RoleCodeEnum.CUSTOMER.getCode()));
        return DataResponse.success(pickUpOrderBizService.pageOrders(orderQueryParam));
    }

    @GetMapping("/detail/{sysEntryOrderCode}")
    @ApiOperation(value = "查询提货申请详情接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<PickUpOrderDetailVO> getOrderDetail(@PathVariable("sysEntryOrderCode") String sysEntryOrderCode) {
        List<String> userRoles = LhSessionContext.getUserRoles();
        boolean customerRole = userRoles.contains(RoleCodeEnum.CUSTOMER.getCode());
        return DataResponse.success(pickUpOrderBizService.getOrderDetail(sysEntryOrderCode, customerRole));
    }

    @PostMapping(value = "/line/excel/{sysSupplierId}")
    @ApiOperation(value = "提货单商品信息excel导入")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderLineExcel", value = "订单商品excel文件", required = true,
                    dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query")
    })
    public DataResponse<List<EntryOrderLineImportResultVO>> importEntryOrderLines(@PathVariable("sysSupplierId") String sysSupplierId,
                                                                                  @RequestParam("orderLineExcel") MultipartFile orderLineExcel) {
        String userId = LhSessionContext.getUserId();
        return DataResponse.success(pickUpOrderBizService.importEntryOrderLines(userId, sysSupplierId, orderLineExcel));
    }

    @PostMapping
    @ApiOperation(value = "新增提货申请接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<Void> save(@Valid @RequestBody PickUpOrderSaveOrUpdateParam saveParam) {
        pickUpOrderBizService.addPickUpOrder(saveParam);
        return DataResponse.success();
    }

    @PutMapping("/{sysEntryOrderCode}")
    @ApiOperation(value = "编辑提货申请接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> edit(@PathVariable("sysEntryOrderCode") String sysEntryOrderCode,
                                   @Valid @RequestBody PickUpOrderSaveOrUpdateParam editParam) {

        List<String> userRoles = LhSessionContext.getUserRoles();
        boolean customerRole = userRoles.contains(RoleCodeEnum.CUSTOMER.getCode());
        editParam.setSysEntryOrderCode(sysEntryOrderCode);
        editParam.setCustomerRole(customerRole);
        pickUpOrderBizService.editPickUpOrder(editParam);
        return DataResponse.success();
    }

    @GetMapping("/entry/result/{sysEntryOrderCode}")
    @ApiOperation(value = "查询提货结果接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<List<EntryOrderResultVO>> getEntryOrderResult(@PathVariable("sysEntryOrderCode") String sysEntryOrderCode) {
        return DataResponse.success(pickUpOrderBizService.getEntryOrderResult(sysEntryOrderCode));
    }


    @PostMapping("/cancel/{sysEntryOrderCode}")
    @ApiOperation(value = "取消提货申请接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<Void> cancel(@PathVariable("sysEntryOrderCode") String sysEntryOrderCode) {
        pickUpOrderBizService.cancel(sysEntryOrderCode);
        return DataResponse.success();
    }

    @PutMapping("/state/{sysEntryOrderCode}")
    @ApiOperation(value = "提货单状态变更接口")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<Void> changeState(@PathVariable("sysEntryOrderCode") String sysEntryOrderCode,
                                          @Valid @RequestBody EntryOrderStateChangeParam stateChangeParam) {
        stateChangeParam.setSysEntryOrderCode(sysEntryOrderCode);
        pickUpOrderBizService.changeState(stateChangeParam);
        return DataResponse.success();
    }

    @PutMapping("/mark")
    @ApiOperation(value = "标记完成接口")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE})
    public DataResponse<Void> markComplete(@Valid @RequestBody MarkCompletionParam markCompletionParam) {
        pickUpOrderBizService.markComplete(markCompletionParam);
        return DataResponse.success();
    }

}
